using UnityEngine;
using System.Collections;

public class PlayerWControl : MonoBehaviour {
	
	public GUIText element;
	public GameObject firepoint;
	public GameObject ammoObj;
	public int ammo = 10;
	const float maxhp = 100;
	const float maxMana = 50;
	public Texture globepic;
	public Texture globeBasePic;
	public Texture globepic2;
	public float hp = maxhp;
	public float mana = maxMana;
	public GUIStyle font;
	
	private Weapon weapon;
	private float globeHeight = 0;	
	private int globesize = 172;
	private int TextSize = 200;
	private float healthpercent = 0;
	private float InicialGH;
	private float InicialTGH;
	
	// Use this for initialization
	void Start () {
		weapon = new MainTankGun(this, 10, 10, 10, 10, firepoint, ammoObj);
		InicialGH = hp /maxhp * globesize;
		InicialTGH = hp /maxhp * TextSize;
		animation.wrapMode=WrapMode.Loop;
		element.GetComponent<ammoDisplay>().setAmmo(ammo);
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)){
			if(ammo > 0) {
    			weapon.Fire();
				ammo -= 1;
				element.GetComponent<ammoDisplay>().setAmmo(ammo);
			}
    	}
	}
	
	void OnGUI () {
		healthpercent = hp /maxhp;
		if(healthpercent < 0) {
			healthpercent = 0;
		} else {
			if(healthpercent > 100) {
				healthpercent = 100;
			}
		}
		globeHeight = healthpercent * globesize;
		
		//DEBUG TEXT
		GUI.BeginGroup(new Rect(10, Screen.height - (InicialTGH + 220), TextSize, TextSize));
		GUI.TextArea(new Rect(0, -TextSize + InicialTGH, TextSize, TextSize), "DEBUG\n" + "HP: " + hp + "\nAmmo: " + ammo);
		GUI.EndGroup();
		
		//HEALTH ORB
		Rect text0 = new Rect(20, Screen.height - (InicialGH + 20), globesize, globesize);
		GUI.BeginGroup(text0);
		GUI.DrawTexture(new Rect(0, -globesize + InicialGH, globesize, globesize), globeBasePic);
		GUI.EndGroup();
		
		GUI.BeginGroup(new Rect(20, Screen.height - (globeHeight + 20), globesize, globesize));
		GUI.DrawTexture(new Rect(0, -globesize + globeHeight, globesize, globesize), globepic);
		GUI.EndGroup();
		
		GUI.Label(new Rect(text0.xMin + globesize/2 - 37, text0.yMin + globesize/2 - 20, text0.width, text0.height), "" + (int)hp, font);

		//MANA ORB
		Rect text1 = new Rect(Screen.width - (InicialGH + 20), Screen.height - (InicialGH + 20), globesize, globesize);
		GUI.BeginGroup(text1);
		GUI.DrawTexture(new Rect(0, -globesize + InicialGH, globesize, globesize), globeBasePic);
		GUI.EndGroup();
		
		GUI.BeginGroup(new Rect(Screen.width - (InicialGH + 20), Screen.height - (InicialGH + 20), globesize, globesize));
		GUI.DrawTexture(new Rect(0, -globesize + InicialGH, globesize, globesize), globepic2);
		GUI.EndGroup();
		
		GUI.Label(new Rect(text1.xMin + globesize/2 - 37, text1.yMin + globesize/2 - 20, text1.width, text1.height), "" + (int)mana, font);
		
	}
	
	public void increaseAmmo(int v) {
		ammo += v;
		element.GetComponent<ammoDisplay>().setAmmo(ammo);
	}
	
	public void decreaseHealth(float damage) {
		hp -= damage;
		if (hp <=0) {
			//hp a 0 objectivo é so destruir o gameobject não o script, posterior imp de respawn.
			hp = 0;
			Destroy(transform.gameObject);
		}		
	}
}
